package com.usermanager.dao.impl;

import java.beans.Statement;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.system.dao.DBDAO;
import com.usermanager.dao.IUser;
import com.usermanager.entity.UserVO;

public class UserImpl implements IUser {
	private Connection conn;
	private Statement st;
	private PreparedStatement pst;
	private ResultSet rs;

	public int addUser(UserVO userVO) throws SQLException {
		conn = DBDAO.getConnection();
		
		return 0;
	}

	public int deleteUser(UserVO userVO) throws SQLException {
		return 0;
	}
	
	public int update(UserVO userVO) throws SQLException {
		return 0;
	}

	public List<UserVO> selectAllUser() throws SQLException {
		List<UserVO> users = new ArrayList<UserVO>();
		conn = DBDAO.getConnection();
		String sql = "SELECT * FROM userinfo ";
		conn.createStatement();
		pst = conn.prepareStatement(sql);
		rs = pst.executeQuery();
		while(rs.next()){
			UserVO user = new UserVO(rs.getInt("uuid"), rs.getString("username"), rs.getByte("sex"), rs.getInt("age"));
			users.add(user);
		}
		return users;
	}
	

	public List<UserVO> selectById(UserVO userVO) throws SQLException {
		List<UserVO> users = new ArrayList<UserVO>();
		conn = DBDAO.getConnection();
		String sql = "SELECT * FROM userinfo WHERE uuid = ?";
		conn.createStatement();
		pst = conn.prepareStatement(sql);
		pst.setInt(1, userVO.getUuid());
		rs = pst.executeQuery();
		while(rs.next()){
			UserVO user = new UserVO(rs.getInt("uuid"), rs.getString("username"), rs.getByte("sex"), rs.getInt("age"));
			users.add(user);
		}
		return users;
	}

	public static void main(String[] args) throws SQLException {
		List<UserVO> users = new UserImpl().selectAllUser();
		for (UserVO userVO : users) {
			System.out.println(userVO.getUsername());
		}
	}
}
